import java.util.Random;

/**
 * @Author:DarknessShadow
 * @description:
 * @date 2020/5/17 23:47
 */
public class ProStrategy implements Strategy {
    private Random random;
    private int prevHandValue = 0;
    private int currentHndValue = 0;
    private int[][] history = {
            {1, 1, 1,},
            {1, 1, 1,},
            {1, 1, 1,},
    };

    public ProStrategy(int seed) {
        this.random = new Random(seed);
    }

    @Override
    public Hand nextHand() {
        int bet = random.nextInt(getSum(currentHndValue));
        int handvalue = 0;
        if (bet < history[currentHndValue][0]) {
            handvalue = 0;
        } else if (bet < history[currentHndValue][0] + history[currentHndValue][1]) {
            handvalue = 1;
        } else {
            handvalue = 2;
        }
        prevHandValue = currentHndValue;
        currentHndValue = handvalue;
        return Hand.getHand(handvalue);
    }

    private int getSum(int hv) {
        int sum = 0;
        for (int i = 0; i < 3; i++) {
            sum += history[hv][i
                    ];
        }
        return sum;
    }
    @Override
    public void study(boolean win) {
        if (win) {
            history[prevHandValue][currentHndValue]++;
        } else {
            history[prevHandValue][(currentHndValue + 1) % 3]++;
            history[prevHandValue][(currentHndValue + 2) % 3]++;
        }
    }
}
